// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/'
import * as types from './store/mutation-types.js'
import axios from 'axios'
import './assets/css/reset.css'
import '../static/font/iconfont.css'
import './config/rem.js'
import 'mint-ui/lib/style.css'
import { Field, Radio, Switch, Cell, Picker, DatetimePicker } from 'mint-ui'
Vue.component(Field.name, Field)
Vue.component(Radio.name, Radio)
Vue.component(Switch.name, Switch)
Vue.component(Cell.name, Cell)
Vue.component(Picker.name, Picker)
Vue.component(DatetimePicker.name, DatetimePicker)
import vuefilter from './store/filter.js'
vuefilter.vueFilter()
Vue.use(require('vue-wechat-title'))
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    // 1.从其他页到这个页面时，获取token
    var token = ''
    // 2.初次打开页面，获取token
    // 查找一个值，可以把它转换为数组，通过循环、对比来查找
    var arr = document.cookie.split(' ')
    // 不要忘记空格!!!
    for (var i in arr) {
      var acookie = arr[i]
      // 某一条cookie,是一个字符串，可再次分割为数组
      var arr2 = acookie.split('=')//
      if (arr2[1] !== undefined && arr2[1] !== 'undefined' && arr2[1] !== null && arr2[1] !== '') {
        if (arr2[0] === 'ylUserInfo') {
          // 找到了你要找的那个cookie的key
          token = arr2[1]
        }
      }
    }
    // alert(token)
    console.log(token)
    // 检测token是否正确
    axios.get('https://prem2.yaolaivip.com/api/token/verificationToken', {
      params: {
        token: token
        // token: 'cc6d0a55-2e33-47cb-abeb-ae534772b0dc'
      }
    })
    .then(function (response) {
      // token正确
      if (response.data.code === '9999') {
        next()
      }
      // token不正确
      if (response.data.code === '7001' || response.data.code === 7001) {
        store.commit(types.AUTHORIZED, {path: to.fullPath})
      }
    })
    .catch(function (error) {
      console.log(error)
    })
  } else {
    next()
  }
  next()
})

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  template: '<App/>',
  components: { App }
})
